package com.baosight.sczx.zm.cs.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.cs.EngineeringDesign;
import com.baosight.sczx.zm.cs.domain.ZMCSEngineeringDesign;

/**
 * <p>Title: ServiceZMCS61.java</p>
 * <p>Description: 工程建模-工程设计服务类</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张小飞
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-7 下午01:17:59  张小飞  Create
 *</pre>
 */
public class ServiceZMCS61 extends ServiceEPBase {
	
	/**
	 * 初始化数据
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS97</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-10下午05:43:44 余虎
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) 
	{
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		
		return outInfo;
	}
	
	/**
	 * 查询方法
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCSEngineeringDesigne</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-10上午11:33:58 路涛
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) 
	{
		String fdMineId = inInfo.getString("fdParentId");//矿井ID
		StaticTools.setQueryParameters(inInfo, "fdMineId", fdMineId, true);//添加查询条件
		EiInfo outInfo = super.query(inInfo, "ZMCSEngineeringDesign.query", new ZMCSEngineeringDesign());
		
		return outInfo;
	}
	
	/**
	 * 新增方法
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCSEngineeringDesigne</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-10上午11:34:17 路涛
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) 
	{
		//校验名称、序号是否重复
		boolean validate = validate(inInfo, "ZMCSEngineeringDesign.validate", new HashMap<String, String>());
		
		if(validate) 
		{
			return inInfo;
		}
		else if(validateType(inInfo, "ZMCSEngineeringDesign.validateType", new HashMap<String, String>())) {
			//工作面、巷道类型的只能存在一个
			return inInfo;
		}
		else {
			inInfo.getBlock("result").setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("T_CI_ENGINEERINGDESIGNE"));//主键
			inInfo.getBlock("result").setCell(0, "fdMineId", inInfo.getString("fdMineId"));//矿井ID
			EiInfo outinfo = super.insert(inInfo, "ZMCSEngineeringDesign.insert", "result");
			return outinfo;
		}
	}
	
	/**
	 * 修改方法
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCSEngineeringDesigne</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-10上午11:34:26 路涛
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) 
	{
		//校验名称、简称是否重复
		Map<String, String> params = new HashMap<String, String>();
		params.put("fdKeyId", inInfo.getBlock("result").getCellStr(0, "fdKeyId"));
		
		boolean validate = validate(inInfo, "ZMCSEngineeringDesign.validate", params);
		if(validate)
		{
			return inInfo;
		}
		else if(validateType(inInfo, "ZMCSEngineeringDesign.validateType", params)){
			//工作面、巷道类型的只能存在一个
			return inInfo;
		}
		else {
			inInfo.getBlock("result").setCell(0, "fdMineId", inInfo.getString("fdMineId"));//矿井ID
			EiInfo outInfo = super.update(inInfo, "ZMCSEngineeringDesign.update");
			return outInfo;
		}
	}
	
	/**
	 * 删除方法
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCSEngineeringDesigne</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-10上午11:34:32 路涛
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) 
	{
		String fdKeyId = inInfo.getCellStr("result", 0, "fdKeyId");
		boolean validate = EngineeringDesign.validateHasChildren(fdKeyId);
		if(validate) {
			inInfo.setStatus(-1);
			inInfo.setMsg("选择的工程设计下已经存在工作面、巷道或其他，不能删除！");
			return inInfo;
		}
		else {
			EiInfo _temp = super.delete(inInfo, "ZMCSEngineeringDesign.delete");
			EiInfo outInfo = this.query(inInfo);
			outInfo.setMsg(_temp.getMsg());
	        outInfo.setDetailMsg(_temp.getDetailMsg());
			return outInfo;
		}
	}

	/**
	 * 验证名称\序号是否重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-10下午01:50:00 路涛
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		
		String fdName = inInfo.getBlock("result").getCellStr(0, "fdName");
		String fdSerialNumber = inInfo.getBlock("result").getCellStr(0, "fdSerialNumber");
		String fdMineId = inInfo.getBlock("result").getCellStr(0, "fdMineId");
		params.put("fdName", fdName);
		params.put("fdSerialNumber", fdSerialNumber);
		params.put("fdMineId", fdMineId);
		
		List result = dao.query(sqlName, params);
		if(result != null && result.size() > 0) {
			inInfo.setStatus(-1);
			ZMCSEngineeringDesign _temp = (ZMCSEngineeringDesign)result.get(0);
			
			StringBuffer msg = new StringBuffer("提交失败，");
			if(_temp.getFdName() != null && _temp.getFdName().equals(fdName)) {
				msg.append(" 名称 ");
			}
			else {
				msg.append(" 序号 ");
			}
			msg.append("重复！");
			
			inInfo.setMsg(msg.toString());
			return true;
		}
		return false;
	}
	
	/**
	 * 工作面、巷道类型是否重复
	 * <p>Function: validateType</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-12上午07:18:03 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes" })
	private boolean validateType(EiInfo inInfo, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		
		String fdType = inInfo.getBlock("result").getCellStr(0, "fdType");
		String fdMineId = inInfo.getString("fdMineId");
		
		if(fdType != null) {
			params.put("fdType", fdType);
			params.put("fdMineId", fdMineId);
			
			List result = dao.query(sqlName, params);
			if(result!=null && result.size() > 0) {
				inInfo.setStatus(-1);
				
				StringBuffer msg = new StringBuffer("提交失败，");
				if(fdType.equals("1")) {
					msg.append("已经存在类型为“工作面”的工程设计！");
				}
				else if(fdType.equals("2")) {
					msg.append("已经存在类型为“巷道”的工程设计！");
				}
				else {
					msg.append("已经存在类型为“其他”的工程设计！");
				}
				
				inInfo.setMsg(msg.toString());
				return true;
			}
		}

		return false;
	}
}
